package com.pro.service.imple;

import com.pro.mapper.ToExamineMapper;
import com.pro.pojo.*;
import com.pro.service.ToExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 谭金诚
 * @Description TODO
 * @Date 2022/1/14 16:57
 * @Version 1.0
 */
@Service
public class ToExamineServiceImple implements ToExamineService {

    @Autowired
    private ToExamineMapper mapper;

    //查询审核微圈数
    @Override
    public Map<String, Integer> getMicrocircleNums() {
        Map<String, Integer> map = new HashMap<>();
        Integer allMicrocircleNums = mapper.getAllMicrocircleNums("",4);//全部微圈数
        Integer microcircleAdoptNums = mapper.getAllMicrocircleNums("1", 4);//type：4 微圈 审核通过
        Integer microcircleWaitNums = mapper.getAllMicrocircleNums("2" ,4);//type：4 微圈 待审核
        Integer microcircleNoAdoptNums = mapper.getAllMicrocircleNums("3", 4);//type：4 微圈 审核未通过
        map.put("allMicrocircleNums",allMicrocircleNums);
        map.put("microcircleAdoptNums",microcircleAdoptNums);
        map.put("microcircleWaitNums",microcircleWaitNums);
        map.put("microcircleNoAdoptNums",microcircleNoAdoptNums);
        return map;
    }

    //查询审核微圈信息
    @Override
    public List<JsMicrocircle> getAllMicroCircle(JsMicrocircle jsMicrocircle) {
        jsMicrocircle.setType(4);//微圈
        List<JsMicrocircle> allMicroCircle = mapper.getAllMicroCircle(jsMicrocircle);
        return allMicroCircle;
    }

    //根据微圈id查询对应审核微圈信息
    @Override
    public JsMicrocircle getToExamineMicroCircleById(JsMicrocircle jsMicrocircle) {
        jsMicrocircle.setType(4);//微圈
        JsMicrocircle toExamineMicroCircleById = mapper.getToExamineMicroCircleById(jsMicrocircle);
        return toExamineMicroCircleById;
    }

    //审核微圈
    @Transactional
    @Override
    public boolean updateCourseRecord(JsCourseRecord courseRecord) {
        courseRecord.setImplementDate(new Date());
        boolean flag = mapper.updateCourseRecord(courseRecord);
        if (flag){
            if (courseRecord.getResult() == 1){
                //修改微圈为已通过
                boolean flagTwo = mapper.updateShenHe(courseRecord.getMicrocircleId(),3);//3：已通过审核
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }else if (courseRecord.getResult() == 3){
                //修改微圈微已驳回
                boolean flagTwo = mapper.updateShenHe(courseRecord.getMicrocircleId(),6);//3：驳回审核
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }
        }
        return false;
    }

    //添加微圈审核日志
    @Override
    public boolean addAuditLog(JsAuditLog jsAuditLog) {
        jsAuditLog.setAduitId(jsAuditLog.getAduitId());
        jsAuditLog.setCreateDate(new Date());
        boolean flag = mapper.addAuditLog(jsAuditLog);
        return flag;
    }

    //查询审核商品数
    @Override
    public Map<String, Integer> getProductNums() {
        Map<String, Integer> map = new HashMap<>();
        Integer allMicrocircleNums = mapper.getAllMicrocircleNums("",3);//全部商品数
        Integer microcircleAdoptNums = mapper.getAllMicrocircleNums("1", 3);//type：3 商品 审核通过
        Integer microcircleWaitNums = mapper.getAllMicrocircleNums("2" ,3);//type：3 商品 待审核
        Integer microcircleNoAdoptNums = mapper.getAllMicrocircleNums("3", 3);//type：3 商品 审核未通过
        map.put("allMicrocircleNums",allMicrocircleNums);
        map.put("microcircleAdoptNums",microcircleAdoptNums);
        map.put("microcircleWaitNums",microcircleWaitNums);
        map.put("microcircleNoAdoptNums",microcircleNoAdoptNums);
        return map;
    }

    //查询审核商品
    @Override
    public List<Product> getAllProduct(Product product) {
//        product.setType(3);//商品
        List<Product> allProduct = mapper.getAllProduct(product);
        return allProduct;
    }

    //根据商品id查询对应商品审核信息
    @Override
    public Product getToExamineProductById(Product product) {
        product.setType(3);//商品
        Product toExamineProductById = mapper.getToExamineProductById(product);
        return toExamineProductById;
    }

    //审核商品
    @Transactional
    @Override
    public boolean updateProductCourseRecord(JsCourseRecord courseRecord) {
        courseRecord.setImplementDate(new Date());
        boolean flag = mapper.updateCourseRecord(courseRecord);
        if (flag){
            if (courseRecord.getResult() == 1){
                //修改微圈为已通过
                boolean flagTwo = mapper.updateProductState(courseRecord.getProductId(),1);//1 上架
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }else if (courseRecord.getResult() == 3){
                //修改微圈微已驳回
                boolean flagTwo = mapper.updateProductState(courseRecord.getProductId(),2);//2 下架
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }
        }
        return false;
    }

    //高级检索——商品分类
    @Override
    public List<ProductCategroy> getProductClass() {
        List<ProductCategroy> productClass = mapper.getProductClass();
        return productClass;
    }

    //高级检索——商品
    @Override
    public List<Product> getSeniorProduct(Product product) {
        List<Product> seniorProduct = mapper.getSeniorProduct(product);
        return seniorProduct;
    }

    //查询审核课程数
    @Override
    public Map<String, Integer> getCourseNums() {
        Map<String, Integer> map = new HashMap<>();
        Integer allMicrocircleNums = mapper.getAllMicrocircleNums("",2);//全部课程数
        Integer microcircleAdoptNums = mapper.getAllMicrocircleNums("1", 2);//type：2 课程 审核通过
        Integer microcircleWaitNums = mapper.getAllMicrocircleNums("2" ,2);//type：2 课程 待审核
        Integer microcircleNoAdoptNums = mapper.getAllMicrocircleNums("3", 2);//type：2 课程 审核未通过
        map.put("allMicrocircleNums",allMicrocircleNums);
        map.put("microcircleAdoptNums",microcircleAdoptNums);
        map.put("microcircleWaitNums",microcircleWaitNums);
        map.put("microcircleNoAdoptNums",microcircleNoAdoptNums);
        return map;
    }

    //查询审核课程
    @Override
    public List<JsCoursePlus> getAllCourse(JsCoursePlus jsCoursePlus) {
        jsCoursePlus.setType(2);//课程
        List<JsCoursePlus> allCourse = mapper.getAllCourse(jsCoursePlus);
        return allCourse;
    }

    //根据课程id查询对应审核课程信息
    @Override
    public JsCoursePlus getToExamineCourseById(Integer courseId) {
        JsCoursePlus toExamineCourseById = mapper.getToExamineCourseById(courseId);
        return toExamineCourseById;
    }

    //审核课程
    @Override
    public boolean updateCourseCourseRecord(JsCourseRecord courseRecord) {
        courseRecord.setImplementDate(new Date());
        boolean flag = mapper.updateCourseRecord(courseRecord);
        if (flag){
            if (courseRecord.getResult() == 1){
                //修改微圈为已通过
                boolean flagTwo = mapper.updateCourseState(courseRecord.getCourseId(),2,4);//2 上架 4 通过
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }else if (courseRecord.getResult() == 3){
                //修改微圈微已驳回
                boolean flagTwo = mapper.updateCourseState(courseRecord.getCourseId(),3,6);//3 未上架 6 审核失败
                if (flagTwo){
                    return flagTwo;
                }else {
                    return false;
                }
            }
        }
        return false;
    }

    //查询审核日志信息
    @Override
    public List<JsAuditLog> getAllAuditLog(JsAuditLog jsAuditLog) {
        List<JsAuditLog> allAuditLog = mapper.getAllAuditLog(jsAuditLog);
        return allAuditLog;
    }
}
